package logic

import (
	"context"
	"fmt"
	"novel-hub/models/mysql"

	"novel-hub/novelrpc/internal/svc"
	"novel-hub/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelListLogic {
	return &NovelListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelListLogic) NovelList(in *novelrpc.NovelListRequest) (*novelrpc.NovelListResponse, error) {
	// todo: add your logic here and delete this line

	var novel mysql.Novel
	var lists []*novelrpc.NovelList
	var err error
	lists, err = novel.FindNovelByTitleCategory(mysql.DB, in.Title, in.Category, in, lists)
	if err != nil {
		return nil, err
	}
	var total int64
	mysql.DB.Model(&novel).Count(&total)
	fmt.Println(total)

	return &novelrpc.NovelListResponse{
		List:  lists,
		Total: total,
	}, nil
}
